define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    dateFormat(template);
    var rows = 20;

    function initData() {
        getSenDingData(1, rows);
        getSaveData(1, rows);
        getReleaseData(1, rows);
    }

    $(document).on('click', '.query-sending', function () {
        getSenDingData(1, rows);
    });

    $(document).on('click', '.query-save', function () {
        getSaveData(1, rows);
    });

    $(document).on('click', '.query-release', function () {
        getReleaseData(1, rows);
    });

    $(document).on('click', '.check-all-sending', function () {
        if ($(this).is(":checked")) {
            $('input[name="sending-msg-id"]').prop("checked", true);//全选
            $('#tab-sending tbody tr').addClass('tr-click');
        } else {
            $('input[name="sending-msg-id"]').prop("checked", false);//取消全选
            $('#tab-sending tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-save', function () {
        if ($(this).is(":checked")) {
            $('input[name="save-msg-id"]').prop("checked", true);//全选
            $('#tab-save tbody tr').addClass('tr-click');
        } else {
            $('input[name="save-msg-id"]').prop("checked", false);//取消全选
            $('#tab-save tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-release', function () {
        if ($(this).is(":checked")) {
            $('input[name="release-msg-id"]').prop("checked", true);//全选
            $('#tab-release tbody tr').addClass('tr-click');
        } else {
            $('input[name="release-msg-id"]').prop("checked", false);//取消全选
            $('#tab-release tbody tr').removeClass('tr-click')
        }
    }).on('click', '.del-sending,.del-save', function () {
        var $this = $(this);
        var name = $this.attr('data-name');
        $.confirm({
            body: '确定要删除' + name + '吗',
            okHidden: function () {
                var msgId = $this.attr('data-id');
                var ids = new Array();
                ids[0] = parseInt(msgId);
                $.post('/messages/delSending', {ids: JSON.stringify(ids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('删除成功');
                            initData();
                            break;
                        default :
                            toastError('删除失败');
                    }
                });
            }
        });
    }).on('click', '.del-sending-all,.del-save-all', function () {
        var inputs;
        if ($(this).hasClass('del-sending-all')) {
            inputs = $('input[name="sending-msg-id"]');
        } else {
            inputs = $('input[name="save-msg-id"]');
        }
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要删除的消息');
            return;
        }
        $.confirm({
            body: '确定要删除这些消息号吗',
            okHidden: function () {
                $.post('/messages/delSending', {ids: JSON.stringify(ids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('删除成功');
                            initData();
                            break;
                        default :
                            toastError('删除失败');
                    }
                });
            }
        });
    }).on('click', '.del-release', function () {
        var $this = $(this);
        var name = $this.attr('data-name');
        $.confirm({
            body: '确定要删除' + name + '吗',
            okHidden: function () {
                var id = $this.attr('data-id');
                var ids = new Array();
                ids[0] = parseInt(id);
                $.post('/messages/delSended', {ids: JSON.stringify(ids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('删除成功');
                            initData();
                            break;
                        default :
                            toastError('删除失败');
                    }
                });
            }
        });
    }).on('click', '.del-release-all', function () {
        var inputs = $('input[name="release-msg-id"]');
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要删除的消息');
            return;
        }
        $.confirm({
            body: '确定要删除这些消息吗',
            okHidden: function () {
                $.post('/messages/delSended', {ids: JSON.stringify(ids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('删除成功');
                            initData();
                            break;
                        default :
                            toastError('删除失败');
                    }
                });
            }
        });
    }).on('click', '.add-save,.add-sending', function () {
        var msgTitle = $.trim($('#msgTitle').val());
        var msgType = $('#msgType').val();
        var msgContent = $.trim($('#msgContent').val());
        var $form = $("#form-addmsg");
        if (msgTitle.length == 0) {
            $form.validate("showError", "msgTitle", "请填写标题", "msgTitleError");
            return;
        }
        if (msgContent.length == 0) {
            $form.validate("showError", "msgContent", "请填写标题", "msgContentError");
            return;
        }
        var msgStatus = '1';
        if ($(this).hasClass('add-sending')) {
            msgStatus = '2';
        }
        var data = {
            msgTitle: msgTitle,
            msgType: msgType,
            msgContent: msgContent,
            msgStatus: msgStatus
        }
        $.post('/messages/addMsg', data, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('添加成功');
                    initData();
                    $('#addMsg').modal('hide')
                    break;
                default :
                    toastError('添加失败');
            }
        });

    }).on('blur', '#msgTitle', function () {
        var $form = $("#form-addmsg");
        var msgTitle = $.trim($('#msgTitle').val());
        if (msgTitle.length == 0) {
            $form.validate("showError", "msgTitle", "请填写标题", "msgTitleError");
        } else {
            $form.validate("hideError", "msgTitle", "msgTitleError");
        }
    }).on('blur', '#msgContent', function () {
        var $form = $("#form-addmsg");
        var msgContent = $.trim($('#msgContent').val());
        if (msgContent.length == 0) {
            $form.validate("showError", "msgContent", "请填写标题", "msgContentError");
        } else {
            $form.validate("hideError", "msgContent", "msgContentError");
        }
    }).on('click', '.release-msg-sending-all,.release-msg-save-all', function () {
        var inputs;
        if ($(this).hasClass('release-msg-sending-all')) {
            inputs = $('input[name="sending-msg-id"]');
        } else {
            inputs = $('input[name="save-msg-id"]');
        }
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).prop('checked')) {
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要发布的消息');
            return;
        }
        window.location.href = '/messages/releaseMsg?ids=' + JSON.stringify(ids) + '&time=' + new Date();
    }).on('click', '.release-msg', function () {
        var ids = new Array();
        ids[0] = parseInt($(this).attr('data-id'));
        window.location.href = '/messages/releaseMsg?ids=' + JSON.stringify(ids) + '&time=' + new Date();
    });

    function getSenDingData(page, rows) {
        var msgTitle = $.trim($('#msgTitle1').val());
        var msgContent = $.trim($('#msgContent1').val());
        var msgType = $('#msgType1').val();
        var beginTime = $('#beginTime1').val();
        var endTime = $('#endTime1').val();
        var data = {
            msgTitle: msgTitle,
            msgContent: msgContent,
            msgType: msgType,
            beginTime: beginTime,
            endTime: endTime,
            page: page,
            rows: rows,
            msgStatus: '2'
        };
        $.post('/messages/listSending', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-sending', {list: list});
                    $('#tab-sending tbody').empty();
                    $('#tab-sending tbody').html(html);
                    $('.sending-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getSaveData(page, rows) {
        var msgTitle = $.trim($('#msgTitle3').val());
        var msgContent = $.trim($('#msgContent3').val());
        var msgType = $('#msgType3').val();
        var beginTime = $('#beginTime3').val();
        var endTime = $('#endTime3').val();
        var data = {
            msgTitle: msgTitle,
            msgContent: msgContent,
            msgType: msgType,
            beginTime: beginTime,
            endTime: endTime,
            page: page,
            rows: rows,
            msgStatus: '1'
        };
        $.post('/messages/listSave', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-save', {list: list});
                    $('#tab-save tbody').empty();
                    $('#tab-save tbody').html(html);
                    $('.save-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getReleaseData(page, rows) {
        var msgSendedTitle = $.trim($('#msgSendedTitle').val());
        var msgSendedContent = $.trim($('#msgSendedContent').val());
        var msgType = $('#msgType2').val();
        var beginTime = $('#beginTime2').val();
        var endTime = $('#endTime2').val();
        var data = {
            msgSendedTitle: msgSendedTitle,
            msgSendedContent: msgSendedContent,
            msgType: msgType,
            beginTime: beginTime,
            endTime: endTime,
            page: page,
            rows: rows,
            msgStatus: '0'
        };
        $.post('/messages/listRelease', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-release', {list: list});
                    $('#tab-release tbody').empty();
                    $('#tab-release tbody').html(html);
                    $('.release-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    $('.sending-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getSenDingData(num, rows)
        }
    });

    $('.save-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getSaveData(num, rows)
        }
    });

    $('.release-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getReleaseData(num, rows)
        }
    });

    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    initData();
});